namespace :market do
	task :open => :environment do
		include Engine
		Flag.open
		statuses = Status.find(:all) 
		for status in statuses
			if status.open == 1
				status.open_now = 1
			end
			history = History.top(status.stock_id)
			stock = Stock.find(status.stock_id)
			if !history.nil?
				stock.days_opening = history.close
			else
				stock.days_opening = stock.market_value
			end
			stock.save!
			history_new = History.new(:stock_id => status.stock_id, :date => Date.today, :volume => 0)
			history_new.high = history_new.low = history_new.open = stock.market_value
			history_new.save!			
		end		
		engine
	end

	task :close => :environment do
		include Engine
		Flag.close
		statuses = Status.find(:all) 
		for status in statuses
			if status.open == 1
				status.open_now = 0
			end
			history = History.top(status.stock_id)
			stock = Stock.find(status.stock_id)
			history.close = stock.market_value
			history.save!
		end
		#graph
	end
	
	task :equalize => :environment do
		users = User.find(:all)
		for user in users
			if user.id != 1
				stocks = Ownership.my_stocks(user.id)
				total = 0
				for stock in stocks
					dummy, value = Ownership.details(stock.stock_id)
					total += value*stock.quantity
				end
				#puts total
				user.cash = 1000000 - total
				user.save!
			end
		end
	end
	
	task :graph => :environment do
		include Engine
		graph_today
	end
	
end
